package simplyrugby.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import simplyrugby.common.Result;
import simplyrugby.entity.Skill;
import simplyrugby.service.SkillService;

import java.util.List;

@RestController
@RequestMapping("/api/skill")
@RequiredArgsConstructor
public class SkillController {
    private final SkillService skillService;


    @PostMapping
    public Result add(@RequestBody Skill skill) {
        boolean save = skillService.save(skill);
        return Result.success().data(save);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable String id) {
        boolean b = skillService.removeById(id);
        return Result.success().data(b);
    }

    @PutMapping
    public Result updateById(@RequestBody Skill skill) {
        boolean flag = skillService.updateById(skill);
        return flag ? Result.success() : Result.fail();
    }


    @GetMapping("/{id}")
    public Result details(@PathVariable String id) {
        Skill skill = skillService.getById(id);
        return Result.success().data(skill);
    }

    @GetMapping("/list")
    public Result list() {
        List<Skill> list = skillService.list();
        return Result.success().data(list);
    }

    @PostMapping("/page")
    public Result page(@RequestParam(required = false) Integer pageNum,
                       @RequestParam(required = false) Integer pageSize,
                       @RequestBody Skill skill
    ) {
        Page<Skill> page = skillService.list(pageNum, pageSize, skill);
        return Result.success().data(page);
    }
}

